package page;

import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.LinkedHashMap;
import java.util.Map;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.sql.DataSource;

import ressource.CadreAnnonce;
import ressource.CadreProduit;
import ressource.CadreVendeur;

/**
 * Servlet implementation class VueVendeur
 */
public class VueVendeur extends HttpServlet {
	private static final long serialVersionUID = 1L;
	
    /**
     * @see HttpServlet#HttpServlet()
     */
    public VueVendeur() {
        super();
    }
    
	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String sid = request.getParameter("identifiant");
		int id = 0;
		Connection con = null;
		CadreVendeur v;
		Map<CadreProduit, CadreAnnonce> annonces = new LinkedHashMap<CadreProduit, CadreAnnonce>();
		CadreProduit p;
		CadreAnnonce a;
		
		if(sid != null){
			try{
				id = Integer.parseInt(sid);
			}catch(Exception e){}
			
			try{
				Context namingContext = new InitialContext();
				DataSource dataSource = (DataSource)namingContext.lookup("java:comp/env/jdbc/MiniIBay");
				con = dataSource.getConnection();
				
				PreparedStatement pstmt=con.prepareStatement("SELECT `pseudo`, `date_creation`, `note`, `acceptation`, `nb_ventes`, `avatar` FROM `membre` WHERE `id_membre`= ?");
				pstmt.setInt(1, id);
				ResultSet rs = pstmt.executeQuery();
				
				if(rs.next()) {
					v = new CadreVendeur();
					v.setPseudo(rs.getString("pseudo"));
					v.setDateCreation(rs.getTimestamp("date_creation"));
					v.setNote(rs.getFloat("note"));
					v.setNbVentes(rs.getInt("nb_ventes"));
					v.setAcceptation(rs.getByte("acceptation"));
					v.setAvatar(rs.getString("avatar"));
					
					request.setAttribute("vendeur", v);
				}
				
				pstmt=con.prepareStatement("SELECT `a`.id_annonce, `p`.id_produit, `p`.intitule, `tp`.nom AS 'type', `p`.photo, `a`.`etat`, `a`.`prix`, `a`.stock, `a`.date_creation, `a`.date_expiration, `a`.commentaire FROM `annonce` a, `produit` p, `type_produit` tp WHERE `a`.id_produit = `p`.id_produit AND `p`.id_type = `tp`.id_type AND `a`.id_vendeur = ?");
				pstmt.setInt(1, id);
				rs = pstmt.executeQuery();
				
				while(rs.next()){
					a = new CadreAnnonce();
					a.setIdAnnonce(rs.getInt("id_annonce"));
					a.setDateCreation(rs.getTimestamp("date_creation"));
					a.setDateExpiration(rs.getDate("date_expiration"));
					a.setStock(rs.getInt("stock"));
					a.setPrix(rs.getFloat("prix"));
					a.setEtat(rs.getString("etat"));
					a.setCommentaire(rs.getString("commentaire"));
					
					p = new CadreProduit();
					p.setId(rs.getInt("id_produit"));
					p.setIntitule(rs.getString("intitule"));
					p.setType(rs.getString("type"));
					p.setPhoto(rs.getString("photo"));
					
					annonces.put(p, a);
				}
				
				request.setAttribute("annonces", annonces);
				
			}catch(Exception e){
				e.printStackTrace();
			} finally {
				try {
					if(con != null) con.close();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
		
		RequestDispatcher dis=request.getRequestDispatcher("vendeur.jsp");
		dis.forward(request,response);
	}
	
	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	}
}
